Solving 10385 - Duathlon (Ternary search)
[and.git] / 11203 - Can you decide it for ME / problem11203_2doIntento.dpr
blob34b642d56c8f08e926a77e38146a4cb84f229021
1 program problem11203_2doIntento (input, output);\r
2 \r
3 {$APPTYPE CONSOLE}\r
4 \r
5 \r
6 Function StrToInt(Const S: String): Integer;\r
7 Var\r
8   E: Integer;\r
9 Begin\r
10   Val(S, Result, E);\r
11 End;\r
14 function ocurrencias(const s: string; const c:char) : integer;\r
15 var\r
16   i : integer;\r
17 begin\r
18 result := 0;\r
19 for i:=0 to length(s) do\r
20   if s[i] = c then\r
21     inc(result);\r
22 end;\r
25 function isTheorem(s : string) : boolean;\r
26 var\r
27   i, x, y, z : integer;\r
28   posM, posE : integer;\r
29 begin\r
30 result := false;\r
31 if (ocurrencias(s, 'M') = 1) and (ocurrencias(s, 'E') = 1) and (ocurrencias(s, '?') >= 4) then\r
32   begin\r
33   x := 0; y := 0; z := 0;\r
34   posM := pos('M', s);\r
35   for i:=1 to posM-1 do\r
36     if (s[i] = '?') then\r
37       inc(x)\r
38     else\r
39       exit;\r
40   s := copy(s, posM + 1, length(s));\r
41   posE := pos('E', s);\r
42   for i:=1 to posE-1 do\r
43   if (s[i] = '?') then\r
44     inc(y)\r
45   else\r
46     exit;\r
47   s := copy(s, posE + 1, length(s));\r
48   for i:=1 to length(s) do\r
49   if (s[i] = '?') then\r
50     inc(z)\r
51   else\r
52     exit;\r
54   result := (x + y = z) and (x*y*z <> 0);\r
55   end;\r
56 end;\r
58 var\r
59   howMany : integer;\r
60   i, principalLoop : integer;\r
61   line : String;\r
62   correctChars : Set of char;\r
63   invalid : boolean;\r
65 //Main:\r
66 begin\r
67 //reset(input, 'input.txt');\r
68 //reset(output, 'out.txt');\r
69 correctChars := ['M', 'E', '?'];\r
70 readLn(line);\r
71 howMany := strToInt(line);\r
73 for principalLoop := 1 to howMany do\r
74   begin\r
75   invalid := false;\r
76   readLn(line);\r
77   for i := 1 to length(line) do\r
78       if not (line[i] in correctChars) then\r
79         invalid := true;\r
80   if invalid then\r
81     writeLn('no-theorem')\r
82   else\r
83     begin\r
84     if isTheorem(line) then\r
85       writeLn('theorem')\r
86     else\r
87       writeLn('no-theorem');\r
88     end;\r
89   end;\r
90 end.\r